<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form v-loading="showLoading" element-loading-text="上传中" element-loading-spinner="el-icon-loading" ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="ID">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="名称">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="图片地址">
            <div style="display:flex;">
              <el-input v-model="form.url" style="margin-right: 5px"/>
              <el-upload
                ref="upload"
                :limit="1"
                :headers="headers"
                :show-file-list="false"
                :before-upload="beforeUpload"
                :on-success="handleSuccess"
                :on-error="handleError"
                :action="baseApi + '/front/upload/uploadV1'"
              >
                <el-button type="primary">上传图片</el-button>
              </el-upload>
            </div>
            <el-image v-if="form.url" :src="form.url" style="width: 200px;height:100px;"></el-image>
          </el-form-item>
          <el-form-item label="跳转地址">
            <el-input v-model="form.jump" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="附加字段">
            <el-input v-model="form.dictName" style="width: 370px;" />
          </el-form-item>
          <!-- <el-form-item label="创建时间">
            <el-input v-model="form.createdAt" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="修改时间">
            <el-input v-model="form.updatedAt" style="width: 370px;" />
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2 || showLoading" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="url" label="图片地址">
          <div slot-scope="scope" >
            <el-image v-if="scope.row.url" :src="scope.row.url"></el-image>
          </div>
        </el-table-column>
        <el-table-column prop="jump" label="跳转地址" />
        <el-table-column prop="dictName" label="附加字段" />
        <el-table-column prop="createdAt" label="创建时间" />
        <el-table-column prop="updatedAt" label="修改时间" />
        <el-table-column v-if="checkPer(['admin','dccIndex:edit','dccIndex:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDccIndex from '@/api/dccIndex'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

const defaultForm = { id: null, name: null, url: null, jump: null, dictName: null, createdAt: null, updatedAt: null }
export default {
  name: 'DccIndex',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '首页轮播', url: 'api/dccIndex', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDccIndex }})
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      permission: {
        add: ['admin', 'dccIndex:add'],
        edit: ['admin', 'dccIndex:edit'],
        del: ['admin', 'dccIndex:del']
      },
      showLoading: false,
      rules: {
      }    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    beforeUpload (file) {
      let list = ['jpeg','png','jpeg','dds','psd','pdt','webp','xmp','gif','bmp','svg','tiff']
      let type = file.type.split('/')[1] ? file.type.split('/')[1] : ''
      if (list.indexOf(type) === -1) {
        this.$message.error('文件类型不正确')
        return false
      }
      this.showLoading = true
    },
    handleSuccess (response,file,fileList) {
      if (response && response.imgUrl) {
        this.form.url = response.imgUrl
        this.$refs.upload.clearFiles()
        this.showLoading = false
        this.$message.success('上传成功')
      }
    },
    handleError () {
        this.showLoading = false
        this.$message.error('上传失败')
    }
  }
}
</script>

<style scoped>

</style>
